package com.example.shkjdemo.bluetooth

import android.bluetooth.BluetoothSocket
import java.io.InputStream
import java.io.OutputStream

class ConnectThread private constructor(
    private var socket: BluetoothSocket,
    private val activeConnect: Boolean
) : Thread() {
    var inputStream: InputStream? = null
    var outputStream: OutputStream? = null
    var device: OutputStream? = null
    override fun run() { //如果是自动连接 则调用连接方法
        if (activeConnect) {
            try {
                socket = device?.javaClass?.getMethod(
                    "createRfcommSocket", *arrayOf<Class<*>?>(
                        Int::class.javaPrimitiveType
                    )
                )?.invoke(device, 1) as BluetoothSocket
            } catch (e: Exception) {
                e.printStackTrace()
            }
            try {
                sleep(500)
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
        }
    }

}